//
//  CBGLoginViewController.m
//  chebabaGYS
//
//  Created by zhengminjie on 15/12/24.
//  Copyright © 2015年 zhengminjie. All rights reserved.
//

#import "CBGLoginViewController.h"
#import "CBGRequsetServers.h"
#import "CustomDefine.h"

@interface CBGLoginViewController ()
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIView *gexian;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *userPhoneNumTF; //手机号
@property (weak, nonatomic) IBOutlet UITextField *codeTF; // 验证码

//记录倒计时
@property (nonatomic) int number;


@end

@implementation CBGLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //自定义mainview
    [self createMainView];
    
}

- (void)createMainView{
    
    self.gexian.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.15];
    self.mainView.layer.cornerRadius = 5;
    self.mainView.layer.masksToBounds = YES;
    self.mainView.layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.15] CGColor];
    self.mainView.layer.borderWidth = 1;
    
    //获取验证码的button
    _codeBtn.layer.cornerRadius = 5;
    _codeBtn.layer.masksToBounds = YES;
    
}
//获取手机验证码
- (IBAction)getcodeBtn:(UIButton *)sender {
    
    if (IsNilString(self.userPhoneNumTF.text)) {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"请输入手机号码" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [myAlertView show];
        
    }else{
        if (self.userPhoneNumTF.text.length < 11) {
            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号码" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
            [myAlertView show];
        }else{
            /**
             * 移动号段正则表达式
             */
            NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
            /**
             * 联通号段正则表达式
             */
            NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
            /**
             * 电信号段正则表达式
             */
            NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
            NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
            BOOL isMatch1 = [pred1 evaluateWithObject:self.userPhoneNumTF.text];
            NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
            BOOL isMatch2 = [pred2 evaluateWithObject:self.userPhoneNumTF.text];
            NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
            BOOL isMatch3 = [pred3 evaluateWithObject:self.userPhoneNumTF.text];
            
            if (isMatch1 || isMatch2 || isMatch3) {
                
                self.number = 60;
                self.codeBtn.enabled = NO;
                NSLog(@"...");
                //self.getSmsBtn.titleLabel.text = [NSString stringWithFormat:@"重新获取(%d)",self.number];
                [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatGetSms:) userInfo:nil repeats:YES];
                //[NSTimer timerWithTimeInterval:2 target:self selector:@selector(repeatGetSms) userInfo:nil repeats:YES];
                NSLog(@"...");
                
                //发送请求
                NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
                [ud removeObjectForKey:@"userSmscode"];
                //[ud setObject:smscode forKey:@"userSmscode"];
                [CBGRequsetServers getMessageCodeByParameters:@{@"mobilephone":self.userPhoneNumTF.text} andSuccess:^(id sender) {
                    
                } andFailure:^(id sender) {
                    
                }];
                
            }else{
                
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号码" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
                [myAlertView show];
                
            }
            
        }
        
    }
    
}

- (void)repeatGetSms:(NSTimer *)timer{
    
    self.number = self.number - 1;
    //NSLog(@"%d",self.number);
    
    self.codeBtn.titleLabel.text = [NSString stringWithFormat:@"重新获取(%d)",self.number];
    
    if (self.number == 0) {
        
       // self.verCodeLab.text = @"获取验证码";
        self.codeBtn.enabled = YES;
        
        [timer invalidate];
    }
}


- (IBAction)loginUser:(UIButton *)sender {
    
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"CBGHomeViewController"];
    [self.navigationController pushViewController:vc animated:YES];
    //[self.navigationController pushViewController:VC animated:YES];
    if (!IsNilString(self.userPhoneNumTF.text) && !IsNilString(self.codeTF.text)) {
        
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *registerId = [ud objectForKey:@"registerId"];
        NSString *smscode = [ud objectForKey:@"userSmscode"];
        NSLog(@"%@",registerId);
        if (IsEquallString(smscode, self.codeTF.text)) {
            [CBGRequsetServers userLoginByParameters:@{@"user_iph":self.userPhoneNumTF.text,@"channad_id":registerId} andSuccess:^(id sender) {
                [self.navigationController pushViewController:vc animated:YES];
            } andFailure:^(id sender) {
            }];
        }else{
            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"验证码不正确" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
            [myAlertView show];
        }
        
        
    }else{
        
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"请输入验证码" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [myAlertView show];
    }

    
    
    
    
    
}
- (IBAction)tellPhoneNum:(UIButton *)sender {
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
